package com.example.moonshine.redzxingtree.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.example.moonshine.redzxingtree.Data.PostlistData;
import com.example.moonshine.redzxingtree.Data.findWarehouseListData;
import com.example.moonshine.redzxingtree.R;
import com.github.zackratos.ultimatebar.UltimateBar;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

/**
 * Created by moonshine on 2018/2/8.
 */

public class Scoplist extends Activity {
    @SuppressLint("MissingSuperCall")
    ArrayList findWarehouseList = new ArrayList();
    ListView listView;
    PostAdapter postAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.axtivity_saomiao);
        listView = super.findViewById(R.id.list_Saoitem);
        UltimateBar.newColorBuilder()
                .statusColor(Color.parseColor("#e1e1e1")) // 状态栏颜色
                .statusDepth(50)                // 状态栏颜色深度
                .build(this)
                .apply();
        postAdapter = new PostAdapter(getBaseContext(), findWarehouseList);
        listView.setAdapter(postAdapter);
        new Thread(new Runnable() {
            @Override
            public void run() {
//                        Looper.prepare();
//                        postlisthttp();
//                        Looper.loop();
                new AnotherTask().execute("");
            }
        }).start();
    }


    @SuppressLint("StaticFieldLeak")
    private class AnotherTask extends AsyncTask<String, Void, String> {
        @Override
        protected void onPostExecute(String result) {
            //对UI组件的更新操作
            Gson gson = new Gson();
            PostlistData postlistData = gson.fromJson(result,PostlistData.class);

            List<findWarehouseListData> listData = postlistData.getValues();

            for(int i = 0;i<listData.size();i++)
            {
                String id = listData.get(i).getTargetList();
                String name = listData.get(i).getAluminumCode();
                String time = listData.get(i).getWarehousingInDate();
                String oid = listData.get(i).getOrderId();
                findWarehouseListData p = new findWarehouseListData(id,name,time,oid);
                findWarehouseList.add(p);
            }
            try{
                postAdapter.notifyDataSetChanged();
            }catch (Exception e){
                Log.e(TAG, "postlisthttp: ",e );
            }
        }
        @Override
        protected String doInBackground(String... params) {
            //耗时的操作
            String findWarehouseUrl = "http://119.23.219.22:80/element-plc/finished/find-finished-warehouse";

            OkHttpClient client = new OkHttpClient();
            Gson gson = new Gson();
            String json = "";
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            RequestBody requestBody = FormBody.create(JSON,json);//放进requestBoday中
            Request request = new Request.Builder()
                    .url(findWarehouseUrl)
                    .post(requestBody)
                    .build();
            String resultcStatusCode ="";
            try {
                Response response = client.newCall(request).execute();
                //获取后台传输的额status状态码
                resultcStatusCode = response.body().string();

            } catch (IOException e) {
                e.printStackTrace();
                Log.e(TAG, "doInBackground: ",e );
            }
            return resultcStatusCode;

        }
    }



//
//        public void postlisthttp() {
//
//        String url = "http://120.78.137.182/element/find-event";
//
//        OkHttpClient client = new OkHttpClient();
//        Gson gson = new Gson();
//        String json = "";
//        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
//        RequestBody requestBody = FormBody.create(JSON,json);//放进requestBoday中
//        Request request = new Request.Builder()
//                .url(url)
//                .post(requestBody)
//                .build();
//
//        try {
//            Response response = client.newCall(request).execute();
//
//            //获取后台传输的额status状态码
//            String result = response.body().string();
//
//            PostlistData postlistData = gson.fromJson(result,PostlistData.class);
//
//            List<ListData> listData = postlistData.getValues();
//
//            for(int i = 0;i<listData.size();i++)
//            {
//                String id = listData.get(i).getTarget_id();
//                String name = listData.get(i).getEvent_name();
//                String time = listData.get(i).getEvent_date();
//                ListData p = new ListData(id,name,time);
//                postlist.add(p);
//            }
//
//            Log.i(TAG,"显示数据时间:"+listData.get(0).getEvent_date());
//            Log.i(TAG,"显示返回:"+listData.get(0).getEvent_name());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public class PostAdapter extends BaseAdapter {

        private ArrayList<findWarehouseListData> List;
        private Context context;

        PostAdapter(Context context, ArrayList<findWarehouseListData> list) {
            this.context = context;
            this.List = list;
        }

        @Override
        public int getCount() {
            return List.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @SuppressLint("SetTextI18n")
        @Override
        public View getView(final int position, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = view.inflate(context, R.layout.activity_set_item, null);
            }

            TextView tv = view.findViewById(R.id.set_item_id);
            TextView tv1 = view.findViewById(R.id.set_item_name);
            TextView tv2 = view.findViewById(R.id.set_item_time);

            findWarehouseListData data = List.get(position);
            tv.setText(data.getTargetList());
            tv1.setText(data.getAluminumCode());
            tv2.setText(data.getWarehousingInDate());
            return view;
        }
    }
}
